package com.fyy.sentinel.controller;

import com.alibaba.csp.sentinel.AsyncEntry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.fyy.sentinel.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author fyy
 * @Description
 * @Date 2021/7/13 15:27
 */
@RestController
public class TestAsyncController {

    @Autowired
    private TestService testService;

    @RequestMapping(value = "/testAsync")
    public void test() {
        AsyncEntry asyncEntry = null;
        try {
            asyncEntry = SphU.asyncEntry("TestAsync"); //限流入口
            testService.hello(); //调用异步方法
        } catch (BlockException e) {
            System.out.println("系统繁忙！");
        } finally {
            if (null != asyncEntry) asyncEntry.exit(); //限流出口
        }
    }

}
